package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j  //记录日志
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    //查询所有部门
    @GetMapping("/depts")
    public Result selectAllDepts(){
        log.info("查询全部信息");
        /*
        1.控制层获取前端请求,通过逻辑层对象,调用方法
        2.逻辑层接口声明方法,在逻辑层实现类中,调用持久层对象,调用持久层方法
        3.持久层的方法访问数据库
         */
        List<Dept> deptList=deptService.selectAllDepts();
        System.out.println(deptList);
        return Result.success(deptList);
    }

    //根据id删除部门
    @Log
    @DeleteMapping("/depts/{id}")
    public Result deleteDeptById(@PathVariable Integer id){
        log.info("删除部门"+id);
        deptService.deleteDeptById(id);
        return Result.success();
    }

    //新增部门
    @Log
    @PostMapping("/depts")
    public Result addDept(@RequestBody Dept dept){
        log.info("新建部门:{}",dept);
         deptService.addDept(dept);
        return Result.success();
    }
    //根据id查询
    @GetMapping("/depts/{id}")
    public Result selectDeptById(@PathVariable Integer id){
        log.info("查询:{}",id);
        Dept dept=deptService.selectDeptById(id);
        return Result.success(dept);
    }

    //修改部门信息
    @Log
    @PutMapping("/depts")
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);
        deptService.updateDept(dept);
        return  Result.success();
    }

}
